<template>
    <div class="user">
        <userHeader></userHeader>
<!--        <user-order></user-order>-->
        <user-module style="margin-top: 3vh"></user-module>
        <van-cell to="/user/mySaleOrder" is-link url="">我的售卖</van-cell>
        <van-cell to="/myBook" is-link url="">我的图书</van-cell>
<!--        <van-cell  is-link :value="auth" title="认证状态" @click="authByMoney"></van-cell>-->
        <van-cell is-link :value="user.school" title="学校" @click="updateSchool"></van-cell>
        <van-cell @click="loginOut">注销登录</van-cell>
        <van-dialog v-model="show" title="标题" show-cancel-button>
          <img src="https://img01.yzcdn.cn/vant/apple-3.jpg" />
        </van-dialog>
    </div>
</template>

<script>
    import userHeader from './tabbar-user-header.vue'
    import UserModule from "./tabbar-user-module";
    import {ApiService} from "@/axios/apiService";
    import {Dialog} from "vant"

    export default {
        props: {
        },
        components:{
            UserModule,
            userHeader:userHeader,
            // userOrder:userOrder
        },
        data(){
            return{
                auth: "未认证",
                user: {},
                show: false
            }
        },
        mounted() {
          this.ifLogin();
          this.getUserInfo();
          // 判断认证
          if(this.user.type) {
            this.auth = "以认证"
          } else {
            this.auth = "未认证"
          }
        },
        methods: {
          // 登出
          loginOut() {
            document.cookie = "userId=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
            document.cookie = "useName=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
            Dialog.alert({
              title:"系统提示",
              message: "退出登录成功"
            }).then(()=> {
              this.$router.push("/")
            })
          },
          // 押金认证
          authByMoney() {
            const api = new ApiService("/pay/auth")
            api.getParams({
              userId:api.getCookie("userId")
            })
          },
          // 获取用户详情
          getUserInfo() {
            const api = new ApiService("/user")
            api.getParams({
              userId:api.getCookie("userId")
            }).then(res=> {
              this.user = res.data.data;
            })
          },
          // 判断是否登录
          ifLogin() {
            const api =new ApiService("/user");
            var userId = api.getCookie("userId");
            if(userId == null || userId == "") {
              console.log("token is " + userId)
              Dialog.alert({
                message:"请先登录"
              }).then(()=> {
                this.$router.push('login')
              })
            }else {
              console.log(this.token)
            }},
          // 修改用户信息
          updateUser() {
            const api = new ApiService("/user/update")
            api.post(this.user).then(res=>{
              this.user = res.data.user()
            })
          },

      }
    };
</script>
